import numpy as np
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import SGDRegressor


X = 2 * np.random.rand(100,1)
Y = 4 + 3*X +np.random.randn(100,1)
# l1_ratio = 0.2时 l2_ratio为0.8
elasticnet_reg = ElasticNet(alpha=0.1,l1_ratio=0.2,max_iter=30000)
elasticnet_reg.fit(X,Y)
print(elasticnet_reg.predict([[1.5]]))
print(elasticnet_reg.intercept_)
print(elasticnet_reg.coef_)


# 默认l1_ratio = 0.15
"""
    def __init__(self, loss="squared_loss", *, penalty="l2", alpha=0.0001,
                 l1_ratio=0.15, fit_intercept=True, max_iter=1000, tol=1e-3,
                 shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON,
                 random_state=None, learning_rate="invscaling", eta0=0.01,
                 power_t=0.25, early_stopping=False, validation_fraction=0.1,
                 n_iter_no_change=5, warm_start=False, average=False):
"""
sgd_reg = SGDRegressor(penalty="elasticnet",alpha=0.1,max_iter=10000)
# SGDRegressor中fit函数要求y为一维向量 numpy.ravel()讲数组转换为一维向量
sgd_reg.fit(X,Y.ravel())
print("sgd_reg.predict:",sgd_reg.predict([[1.5]]))
print("sgd_reg.intercept_:",sgd_reg.intercept_)
print("sgd_reg.coef_:",sgd_reg.coef_)